/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-06-08     团       the first version
 */

#include <rtthread.h>
#include <u8g2_port.h>
#include <board.h>
#include <stdio.h>
#include "oled.h"

extern unsigned int g_light_value1;
extern unsigned int g_light_value2;
extern unsigned int g_light_value3;
extern unsigned int g_light_average;

/* 构建u8g2对象*/
u8g2_t u8g2;

/**
 * @brief    该函数用于初始化OLED
 * @note     初始化u8g2对象、使能OLED、配置引脚、设置u8g2字体
 * @param    无传入的参数
 * @return   无返回值
 */
void oled_init(void)
{
    u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_rt_gpio_and_delay);
    u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_I2C_CLOCK, OLED_IIC_SCL);
    u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_I2C_DATA, OLED_IIC_SDA);
    u8g2_InitDisplay(&u8g2);
    u8g2_SetPowerSave(&u8g2, 0);
    u8g2_ClearBuffer(&u8g2);
    u8g2_SetFont(&u8g2, u8g2_font_6x12_tr);
    u8g2_ClearBuffer(&u8g2);
    u8g2_SendBuffer(&u8g2);
}

/**
 * @brief    该函数用于显示OLED
 * @note     分别显示3个光照值以及平均值 LED的状态
 * @param    char *p    LED的状态
 * @return   无返回值
 */
void oled_show(char *p)
{
    char light_value_str1[20];
    char light_value_str2[20];
    char light_value_str3[20];
    char light_value_str4[20];

    /*将光照值从无符号整形转换成字符串 方便显示*/
    sprintf(light_value_str1,"%d",g_light_value1);
    sprintf(light_value_str2,"%d",g_light_value2);
    sprintf(light_value_str3,"%d",g_light_value3);
    sprintf(light_value_str4,"Avr:%d",g_light_average);

    u8g2_ClearBuffer(&u8g2);
    /*显示光照值*/
    u8g2_DrawStr(&u8g2,0,16,"Light1");
    u8g2_DrawStr(&u8g2,45,16,"Light2");
    u8g2_DrawStr(&u8g2,90,16,"Light3");
    u8g2_DrawStr(&u8g2,5,32,light_value_str1);
    u8g2_DrawStr(&u8g2,50,32,light_value_str2);
    u8g2_DrawStr(&u8g2,95,32,light_value_str3);
    u8g2_DrawStr(&u8g2,0,50,light_value_str4);

    u8g2_DrawStr(&u8g2,80,50,p);



    u8g2_SendBuffer(&u8g2);

}

